package com.equipment_management_p1.controller;
import com.equipment_management_p1.service.IEquipmentService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.pojo.Entity.Equipment;
import com.pojo.Entity.EquipmentRental;
import com.pojo.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/eqm")
public class EquipmentController extends BaseController{
    @Autowired
    public IEquipmentService equipmentService;
    //@HystrixCommand(fallbackMethod = "hystrixrental")
    @PostMapping(value = "rental")
    public Result<?> rental( Integer eID,Integer eInventory,HttpServletRequest httpServletRequest){
        if(eInventory<1)
            return  Result.failure();
        int count=equipmentService.modifyQuantity(eID,eInventory,null,null,httpServletRequest);
        return  count>0?Result.success(1):Result.failure();
    }
    @PostMapping(value = "prerental")
    public Result<?> prerental( Integer eID,Integer eInventory,String vPretime,String vPreEndtime,HttpServletRequest httpServletRequest){
        if(eInventory<1)
            return  Result.failure();
        int count=equipmentService.modifyQuantity(eID,eInventory,vPretime,vPreEndtime,httpServletRequest);
        return  count>0?Result.success(1):Result.failure();
    }
    @PostMapping ("geteqm")
    public Result<?>geteqm(Integer eID){
        Equipment equipment=equipmentService.getEquipment(eID);
        return equipment==null?Result.failure():Result.success(equipment);
    }

    @PostMapping("getleqm")
    public Result<?>getleqm(Integer page,Integer number,String key){
        List< Equipment> equipment=equipmentService.getlEquipment(page,number,key);
        Result result=Result.success(equipment);
        result.setMessage(String.valueOf(equipmentService.getsum(key)));
        return result;
    }
    @PostMapping("upeqm")
    public Result<?>upeqm(Equipment equipment,HttpServletRequest httpServletRequest){
        Integer count=equipmentService.update(equipment);
        return  count==1?Result.success(1):Result.failure();
    }
    @PostMapping("payeqm")
    public Result<?>payeqm(Integer erID,Integer erType){
        Integer count=equipmentService.updateER(erID,erType,null);
        return count==null?Result.failure():Result.success(count);
    }
    @PostMapping("isreturn")
    public Result<?>isreturn(Integer erID, Integer erReturn, HttpServletRequest httpServletRequest){
        Integer count=equipmentService.updateER(erID,null,erReturn);
        return count==null?Result.failure():Result.success(count);
    }
    @PostMapping("addeqm")
    public Result<?>isreturn(Equipment equipment, HttpServletRequest httpServletRequest){
        Integer count=equipmentService.insert(equipment);
        return count==null?Result.failure():Result.success(count);
    }
    @PostMapping("getlpayrt")
    public Result<?>getlpayrt(Integer page, Integer number, Integer erTYpe,Integer erReturn,HttpServletRequest httpServletRequest){
        Result result=Result.success(equipmentService.getlEquipmentRental(page,number,erTYpe,erReturn,null,httpServletRequest));
        result.setMessage(String.valueOf(equipmentService.getsume(erTYpe,erReturn,null)));
        return result;
    }
    @PostMapping("getlrpay")
    public Result<?>getlrpay(Integer page, Integer number,HttpServletRequest httpServletRequest){
        Result result=Result.success(equipmentService.getlEquipmentRental(page,number,null,null,1,httpServletRequest));
        return result;
    }
    @PostMapping("deleteeqm")
    public Result<?>deleteeqm(Integer eID,HttpServletRequest httpServletRequest){
        Integer count=equipmentService.delete(eID);
        return count==1?Result.success(count):Result.failure();
    }
    public Result<EquipmentRental> hystrixrental( Integer eID,Integer eSum){
        return  Result.failure();
    }
}
